namespace DotNetNuke.UI.UserControls
{
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// SectionHeadControl is a user control that provides all the server code to allow a
    /// section to be collapsed/expanded, using user provided images for the button.
    /// </summary>
    /// <remarks>
    /// To use this control the user must provide somewhere in the asp page the
    /// implementation of the javascript required to expand/collapse the display.
    /// eg:
    /// function __dnn_SectionMaxMin(img, objID)  {
    /// //implemenation code
    /// ....
    /// }
    /// where __dnn_SectionMaxMin is the name of function, that is supplied
    /// in the Attribute/Property javaScript.
    /// </remarks>
    /// <history>
    /// [cnurse]	9/7/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SectionHeadControl : UserControl
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        //("imgIcon")]
        private ImageButton _imgIcon;
        private bool _includeRule;
        private bool _isExpanded;
        private string _javaScript;
        //("lblTitle")]
        private Label _lblTitle;
        private string _maxImageUrl;
        private string _minImageUrl;
        //("pnlRule")]
        private Panel _pnlRule;
        private string _resourceKey;
        private string _section;

        public SectionHeadControl()
        {
            base.PreRender += new EventHandler(this.Page_PreRender);
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this._includeRule = false;
            this._isExpanded = true;
            this._javaScript = "__dnn_SectionMaxMin";
            this._maxImageUrl = "images/plus.gif";
            this._minImageUrl = "images/minus.gif";
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Assign resource key to label for localization
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [VMasanas]	05/11/2004	Move code to Page_Load for localization
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.ResourceKey != "")
                {
                    this.lblTitle.Attributes["resourcekey"] = this.ResourceKey;
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Renders the SectionHeadControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [VMasanas]	04/11/2004	Moved code from Page_Load to PreRender so all other properties are set.
        /// This allows to inject this control dynamically on a page using LoadControl
        /// [jhenning]	09/06/2005 Utilizing ClientAPI EnableMinMax
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_PreRender(object sender, EventArgs e)
        {
            try
            {
                HtmlControl ctl = (HtmlControl) this.Parent.FindControl(this.Section);
                if (ctl != null)
                {
                    DNNClientAPI.EnableMinMax(this.imgIcon, ctl, !this.IsExpanded, this.Page.ResolveUrl(this.MinImageUrl), this.Page.ResolveUrl(this.MaxImageUrl), DNNClientAPI.MinMaxPersistanceType.Page);
                }
                this.pnlRule.Visible = this._includeRule;
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CssClass determines the Css Class used for the Title Text
        /// </summary>
        /// <value>A string representing the name of the css class</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string CssClass
        {
            get
            {
                return this.lblTitle.CssClass;
            }
            set
            {
                this.lblTitle.CssClass = value;
            }
        }

        protected virtual ImageButton imgIcon
        {
           
            get
            {
                return this._imgIcon;
            }
            
            set
            {
                this._imgIcon = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// IncludeRule determines whether there is a horizontal rule displayed under the
        /// header text
        /// </summary>
        /// <value>A string representing true or false</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool IncludeRule
        {
            get
            {
                return this._includeRule;
            }
            set
            {
                this._includeRule = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// IsExpanded determines whether the section is expanded or collapsed.
        /// </summary>
        /// <value>Boolean value that determines whether the panel is expanded (true)
        /// or collapsed (false).  The default is true.</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// [jhenning]	09/06/2005 Utilizing ClientAPI EnableMinMax
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool IsExpanded
        {
            get
            {
                return DNNClientAPI.get_MinMaxContentVisibile(this.imgIcon, !this._isExpanded, DNNClientAPI.MinMaxPersistanceType.Page);
            }
            set
            {
                this._isExpanded = value;
                DNNClientAPI.set_MinMaxContentVisibile(this.imgIcon, !this._isExpanded, DNNClientAPI.MinMaxPersistanceType.Page, value);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// JavaScript is the name of the javascript function implementation.
        /// </summary>
        /// <value>A string representing the name of the javascript function implementation</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string JavaScript
        {
            get
            {
                return this._javaScript;
            }
            set
            {
                this._javaScript = value;
            }
        }

        protected virtual Label lblTitle
        {
           
            get
            {
                return this._lblTitle;
            }
            
            set
            {
                this._lblTitle = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The MaxImageUrl is the url of the image displayed when the contained panel is
        /// collapsed.
        /// </summary>
        /// <value>A string representing the url of the Max Image</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string MaxImageUrl
        {
            get
            {
                return this._maxImageUrl;
            }
            set
            {
                this._maxImageUrl = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The MinImageUrl is the url of the image displayed when the contained panel is
        /// expanded.
        /// </summary>
        /// <value>A string representing the url of the Min Image</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string MinImageUrl
        {
            get
            {
                return this._minImageUrl;
            }
            set
            {
                this._minImageUrl = value;
            }
        }

        protected virtual Panel pnlRule
        {
           
            get
            {
                return this._pnlRule;
            }
            
            set
            {
                this._pnlRule = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The ResourceKey is the key used to identify the Localization Resource for the
        /// title text.
        /// </summary>
        /// <value>A string representing the ResourceKey.</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string ResourceKey
        {
            get
            {
                return this._resourceKey;
            }
            set
            {
                this._resourceKey = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Section is the Id of the DHTML object  that contains the xection content
        /// title text.
        /// </summary>
        /// <value>A string representing the Section.</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string Section
        {
            get
            {
                return this._section;
            }
            set
            {
                this._section = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Text is the name or title of the section
        /// </summary>
        /// <value>A string representing the Title Text.</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/7/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string Text
        {
            get
            {
                return this.lblTitle.Text;
            }
            set
            {
                this.lblTitle.Text = value;
            }
        }
    }
}

